// page/account/address.js
Page({
  data: {
        isHide: 0,
        addressList: [],
        
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function (options) {
      
          var app = getApp()
          var that = this;
          var uid = wx.getStorageSync('uid');
          wx.request({				
            url: app.globalData.host + '&g=App&m=Cart&a=choose', //仅为示例，并非真实的接口地址
            data: { uid: uid },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              console.log(res.data)
              that.setData({
                isHide: 1,
                addressList: res.data.d
              })
            }
          });
  },
  

  setDefault: function (e) {
    var app = getApp()
    var that = this;
    var uid = wx.getStorageSync('uid');
      var arr=that.data.addressList;
      for (var i = 0; i <= arr.length - 1; i++) {
        if (i == e.currentTarget.id) {
          arr[i]['defa'] = 1
          wx.request({
            url: app.globalData.host + '&g=App&m=Cart&a=xgchoose', //仅为示例，并非真实的接口地址
            data: { uid: uid, id: arr[i]['id'] },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '设置成功',
                icon: 'success',
                duration: 2000
              })
            }
          });
        }else{
          arr[i]['defa'] = 0
        }
      }
      that.setData({
        addressList: arr
      })
  }, 
  deladd:function(e){
    var that = this;
    var arr = that.data.addressList;
    var app = getApp()
    var uid = wx.getStorageSync('uid');
    var id = e.currentTarget.dataset.id  
    // console.log(uid)
    // console.log(id)
    wx.showModal({
      title: '提示',
      content: '确认删除地址吗？',
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i <= arr.length - 1; i++) {
            if (arr[i]['id'] == id) {
              arr.splice(i, 1)
              break
            }
          }
          wx.request({
            url: app.globalData.host + '&g=App&m=Cart&a=deladdress', //仅为示例，并非真实的接口地址
            data: { uid: uid, id: id },
            header: {
              'content-type': 'application/json'
            },
            success: function (res) {
              wx.showToast({
                title: '删除成功',
                duration: 1000
              })
            }
          });
          that.setData({
            addressList: arr
          })
        
        } else if (res.cancel) {
          wx.showToast({
            title: '你点击了取消',
            duration: 1000
          })
        }
        
      }
    })
   


  }

})
      